import java.io.IOException;
import java.io.Serializable;

import mobilefeel.AndroidDevice;
import mobilefeel.DeviceListener;
import mobilefeel.MobileFeel;
import mobilefeel.accelerometer.Accelerometer;
import mobilefeel.accelerometer.AccelerometerListener;
import mobilefeel.dto.AccelerometerEvent;
import mobilefeel.dto.TouchEvent;
import mobilefeel.touchscreen.OnClickListener;
import mobilefeel.touchscreen.OnLongClickListener;
import mobilefeel.touchscreen.OnTouchListener;
import mobilefeel.touchscreen.View;
import mobilefeel.touchscreen.ViewListener;



public class Ejemplo implements DeviceListener, AccelerometerListener, ViewListener, OnClickListener, OnLongClickListener, OnTouchListener{

//	private AndroidDevice device;
	private long nanos;
	
	public Ejemplo(){
		MobileFeel android =  new MobileFeel();
		nanos = 0;
		android.addDeviceListener(this);
		try {
			android.listen(51460);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static void main(String[] args) {
		new Ejemplo();	
	}

	@Override
	public void onSensorChanged(AndroidDevice device, AccelerometerEvent event) {
		//System.out.println("Radianes: " + Math.atan2(event.getY(), event.getX()));
		//System.out.println("Grados: " + Math.atan2(event.getY(), event.getX()) * 180 / Math.PI);
		System.out.println("ID: "+ device.getModel() +
				"\tRes: " + event.getResolution() +
				"\tMaxR: " + event.getMaxRange() +
				"\tX: "+ String.format("%.2f", event.getX())+
				"\tY: "+ String.format("%.2f", event.getY())+
				"\tZ: "+ String.format("%.2f", event.getZ())+ 
				"\tT: "+ ((event.getTimestamp()-this.nanos)/1000000L)+ 
				"\tAcc: "+ event.getAccuracy() ) ;
		this.nanos = event.getTimestamp();
		/*try {
			device.disconnect();
		} catch (IOException e) {
			e.printStackTrace();
		}*/
		
	}

	@Override
	public void onDeviceConnected(AndroidDevice device) {
		try {
			System.out.println("Device Connected - ID: " + device.getDeviceId());
			System.out.println("Brand: "+ device.getBrand());
			System.out.println("Device: "+ device.getDevice());
			System.out.println("Display: "+ device.getDisplay());
			System.out.println("Manufacturer: "+ device.getManufacturer());
			System.out.println("Model: "+ device.getModel());
			System.out.println("Product: "+ device.getProduct());
			device.getAccelerometer().setAccelerometerRate(Accelerometer.SENSOR_DELAY_FASTEST);
//			device.getAccelerometer().addAccelerometerListener(this);
			
			device.getTouchScreen().addViewListener(this);
			
			device.sendCustomData("prueba", "Mensaje enviado desde la PC");
			
			//device.disconnect();
			//this.device = device;
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void onDeviceDisconnected(AndroidDevice device) {
		System.out.println("Device Disconnected - ID: " + device.getDeviceId());
		System.out.println("***************************************************************************");
	}

	@Override
	public void onViewRegistered(AndroidDevice device, View view) {
		System.out.println("View Registered - Device ID: " + device.getDeviceId());
		System.out.println("Nombre de la vista: " + view.getName() + "\tID: "+ Integer.toHexString(view.getId()));
		System.out.println("Tamaño de la vista: " + view.getWidth() + "x" + view.getHeight());
		
		try {
	//		view.addOnClickListener(this);
//			view.addOnLongClickListener(this);
			view.addOnTouchListener(this);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void onTouch(View v, TouchEvent e) {
		System.out.println("Touch Action: " + e.getAction() + " en " + v.getName());
		System.out.println("Action Pointer: " + e.getActionPointer());
		System.out.println("Current: ");
		for(int i=0 ; i<e.getPointerCount(); i++ )
			System.out.println("PointerIndex: "+ i + " PointerId: " + e.getPointerId(i) + " X: " + e.getCurrentByIndex(i).getX() + " Y: " + e.getCurrentByIndex(i).getY());
		System.out.println("Event Time: " + e.getEventTime());
		System.out.println("Historical: ");
		for (int j = 0; j<e.getHistorySize(); j++) {
			for(int i=0 ; i<e.getPointerCount(); i++ )
				System.out.println("HPos: " + j + " PointerIndex: "+ i + " PointerId: " + e.getPointerId(i) + " X: " + e.getHistoricalByIndex(i, j).getX() + " Y: " + e.getHistoricalByIndex(i, j).getY());
			System.out.println("Historical Event Time HPos " + j + ": " + e.getHistoricalEventTime(j));
		}
		System.out.println("**************************************");
	}

	@Override
	public void onLongClick(View v) {
		System.out.println("LongClick en " + v.getName());
	}

	@Override
	public void onClick(View v) {
		System.out.println("Click en " + v.getName() + " - DEVICE: " + v.getAndroidDevice().getModel());
	}

	@Override
	public void onCustomDataReceived(AndroidDevice device, String text, Serializable object) {
	}

}
